package com.sunwayworld.basemodule.business.batchrevise.service.impl;

import com.sunwayworld.basemodule.business.batchrevise.bean.LimsBatchReviseBean;
import com.sunwayworld.basemodule.business.batchrevise.bean.LimsBatchSampleProductTempBean;
import com.sunwayworld.basemodule.business.batchrevise.bean.LimsSampleTempBean;
import com.sunwayworld.basemodule.business.batchrevise.service.LimsBatchReviseService;
import com.sunwayworld.basemodule.business.batchrevise.service.LimsBatchSampleProductTempService;
import com.sunwayworld.basemodule.business.batchrevise.service.LimsSampleTempService;
import com.sunwayworld.basemodule.coredata.product.bean.LimsProductMainBean;
import com.sunwayworld.basemodule.coredata.product.service.LimsProductMainService;
import com.sunwayworld.framework.constant.Constant;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.page.MybatisPageHelper;
import com.sunwayworld.framework.mybatis.page.PageRowBounds;
import com.sunwayworld.framework.support.PersistableHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.batchrevise.dao.LimsBatchSampleTempDao;
import com.sunwayworld.basemodule.business.batchrevise.bean.LimsBatchSampleTempBean;
import com.sunwayworld.basemodule.business.batchrevise.service.LimsBatchSampleTempService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsBatchSampleTempServiceImpl implements LimsBatchSampleTempService {

    @Autowired
    private LimsBatchSampleTempDao limsBatchSampleTempDao;

    @Lazy
    @Autowired
    private LimsBatchReviseService batchReviseService;

    @Lazy
    @Autowired
    private LimsSampleTempService sampleTempService;

    @Lazy
    @Autowired
    private LimsBatchSampleProductTempService sampleProductTempService;

    @Lazy
    @Autowired
    private LimsProductMainService productMainService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsBatchSampleTempDao getDao() {
        return limsBatchSampleTempDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsBatchSampleTempBean limsBatchSampleTemp = jsonWrapper.parseUnique(LimsBatchSampleTempBean.class);
        limsBatchSampleTemp.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsBatchSampleTemp);
        return limsBatchSampleTemp.getId();
    }

    @Override
    public void insertSample(RestJsonWrapperBean wrapper) {
        LimsSampleTempBean sampleBean = wrapper.parseUnique(LimsSampleTempBean.class);
        String reviseId = sampleBean.getExt$Item("reviseId");
        String productId = sampleBean.getExt$Item("productId");
        if (productId == null || reviseId == null) return;
        LimsBatchReviseBean reviseBean = batchReviseService.selectById(Long.parseLong(reviseId));
        List<Long> productIdList = Arrays.stream(productId.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        sampleBean.setId(ApplicationContextHelper.getNextIdentity());
        sampleBean.setOriNo(sampleBean.getSampleCode());

        LimsBatchSampleTempBean batchSampleBean = new LimsBatchSampleTempBean();
        batchSampleBean.setId(ApplicationContextHelper.getNextIdentity());
        batchSampleBean.setReviseId(Long.parseLong(reviseId));
        batchSampleBean.setBatchId(reviseBean.getBatchId());
        batchSampleBean.setBatchCode(reviseBean.getBatchCode());
        batchSampleBean.setSampleTempId(sampleBean.getId());
        batchSampleBean.setSampleCode(sampleBean.getSampleCode());

        List<LimsProductMainBean> productMainBeans = productMainService.selectListByIds(productIdList);
        List<LimsBatchSampleProductTempBean> productBeanList = new ArrayList<>();
        for (LimsProductMainBean productMainBean : productMainBeans) {
            LimsBatchSampleProductTempBean productBean = new LimsBatchSampleProductTempBean();
            productBean.setId(ApplicationContextHelper.getNextIdentity());
            productBean.setReviseId(Long.parseLong(reviseId));
            productBean.setBatchId(reviseBean.getBatchId());
            productBean.setBatchCode(reviseBean.getBatchCode());
            productBean.setBatchSampleTempId(batchSampleBean.getId());
            productBean.setSampleTempId(sampleBean.getId());
            productBean.setSampleCode(sampleBean.getSampleCode());
            productBean.setProductId(productMainBean.getId());
            productBean.setProductCode(productMainBean.getProductCode());
            productBean.setProductName(productMainBean.getProductName());
            productBeanList.add(productBean);
        }

        sampleTempService.getDao().insert(sampleBean);
        getDao().insert(batchSampleBean);
        sampleProductTempService.getDao().insert(productBeanList);
    }

    @Override
    public void updateSample(RestJsonWrapperBean wrapper) {
        LimsSampleTempBean sampleBean = wrapper.parseUnique(LimsSampleTempBean.class);
        String productChange = sampleBean.getExt$Item("productChange");
        sampleTempService.getDao().updateIfChanged(sampleBean);
        // 产品发生变化的
        if ("1".equals(productChange)) {
            // 删除原来的产品
            LimsBatchSampleProductTempBean deleteBean = new LimsBatchSampleProductTempBean();
            String batchSampleId = sampleBean.getExt$Item("batchSampleTempId");
            deleteBean.setBatchSampleTempId(Long.parseLong(batchSampleId));
            sampleProductTempService.getDao().deleteBy(deleteBean, "batchSampleTempId");
            // 然后添加新产品
            String productId = sampleBean.getExt$Item("productId");
            List<Long> productIdList = Arrays.stream(productId.split(","))
                    .map(Long::parseLong).collect(Collectors.toList());
            List<LimsProductMainBean> productMainBeans = productMainService.selectListByIds(productIdList);
            List<LimsBatchSampleProductTempBean> productBeanList = new ArrayList<>();
            for (LimsProductMainBean productMainBean : productMainBeans) {
                LimsBatchSampleProductTempBean productBean = new LimsBatchSampleProductTempBean();
                productBean.setId(ApplicationContextHelper.getNextIdentity());
                productBean.setReviseId(Long.parseLong(sampleBean.getExt$Item("reviseId")));
                productBean.setBatchId(Long.parseLong(sampleBean.getExt$Item("batchId")));
                productBean.setBatchCode(sampleBean.getExt$Item("batchCode"));
                productBean.setBatchSampleTempId(Long.parseLong(sampleBean.getExt$Item("batchSampleTempId")));
                productBean.setSampleTempId(sampleBean.getId());
                if(sampleBean.getExt$Item("batchSampleId") != null) {
                    productBean.setBatchSampleId(Long.parseLong(sampleBean.getExt$Item("batchSampleId")));
                }
                productBean.setSampleId(sampleBean.getSampleId());
                productBean.setSampleCode(sampleBean.getSampleCode());
                productBean.setProductId(productMainBean.getId());
                productBean.setProductCode(productMainBean.getProductCode());
                productBean.setProductName(productMainBean.getProductName());
                productBeanList.add(productBean);
            }
            sampleProductTempService.getDao().insert(productBeanList);
        }

    }

    @Override
    public Page<LimsSampleTempBean> selectBatchSampleInfo(RestJsonWrapperBean wrapper) {
        MapperParameter parameter = wrapper.extractMapFilter();
        PageRowBounds pageRowBounds = wrapper.extractPageRowBounds();
        return MybatisPageHelper.get(pageRowBounds, () -> getDao().selectBatchSampleInfo(parameter)
                .parallelStream().map(e -> PersistableHelper.mapToPersistable(e , LimsSampleTempBean.class))
                .collect(Collectors.toList()));
    }

    @Override
    public List<LimsSampleTempBean> selectBatchSampleInfo(MapperParameter parameter) {
        return getDao().selectBatchSampleInfo(parameter)
                .parallelStream().map(e -> PersistableHelper.mapToPersistable(e , LimsSampleTempBean.class))
                .collect(Collectors.toList());
    }


    @Transactional
    @Override
    public void deleteBatchSamples(RestJsonWrapperBean wrapper) {
        List<LimsSampleTempBean> parse = wrapper.parse(LimsSampleTempBean.class);
        List<Long> batchSampleTempIds = parse.stream().map(s -> s.getExt$Item("batchSampleTempId")).map(Long::parseLong).collect(Collectors.toList());
        List<LimsBatchSampleProductTempBean> productBeanList = new ArrayList<>();
        for (Long batchSampleTempId : batchSampleTempIds) {
            LimsBatchSampleProductTempBean productBean = new LimsBatchSampleProductTempBean();
            productBean.setBatchSampleTempId(batchSampleTempId);
            productBeanList.add(productBean);
        }
        sampleProductTempService.getDao().deleteBy(productBeanList, "batchSampleTempId");
        getDao().deleteByIdList(batchSampleTempIds);
        List<LimsSampleTempBean> deleteSamples = parse.stream().filter(s ->
                Constant.NO.equals(s.getExt$Item("selected"))).collect(Collectors.toList());
        sampleTempService.getDao().deleteBy(deleteSamples);
    }
}
